-- Setup nvim-cmp.
local cmp_status, cmp = pcall(require, "cmp")
if not cmp_status then
    return
end


return {{
    'hrsh7th/nvim-cmp',
    dependencies = {
        "hrsh7th/cmp-buffer",             -- 单词补全
        "hrsh7th/cmp-path",               -- 路径补全
        "hrsh7th/cmp-nvim-lsp",           -- 语义分析补全
        -- "quangnguyen30192/cmp-nvim-tags", -- 符号补全  (不生效欸)
        "hrsh7th/cmp-cmdline",
        "hrsh7th/cmp-vsnip",
        "hrsh7th/vim-vsnip",
        "rafamadriz/friendly-snippets",
        "onsails/lspkind-nvim",
    },
    config = function()
        local lspkind_status, lspkind = pcall(require, "lspkind")
        if not lspkind_status then
            return
        end

        cmp.setup {
            snippet = {
                expand = function(args)
                    -- For `ultisnips` user.
                    vim.fn["vsnip#anonymous"](args.body)
                end,
            },
            mapping = cmp.mapping.preset.insert {
                ["<Tab>"] = function(fallback)
                    if cmp.visible() then
                        cmp.select_next_item()
                    else
                        fallback()
                    end
                end,
                ["<S-Tab>"] = function(fallback)
                    if cmp.visible() then
                        cmp.select_prev_item()
                    else
                        fallback()
                    end
                end,
                ["<CR>"] = cmp.mapping.confirm { select = true },
                ["<C-e>"] = cmp.mapping.abort(),
                ["<Esc>"] = cmp.mapping.close(),
                ["<C-d>"] = cmp.mapping.scroll_docs(-4),
                ["<C-f>"] = cmp.mapping.scroll_docs(4),
            },
            sources = {
                { name = "nvim_lsp" }, -- For nvim-lsp
                { name = "ultisnips" }, -- For ultisnips user.
                { name = "path" }, -- for path completion
                { name = "buffer", keyword_length = 2 }, -- for buffer word completion
                { name = "omni" },
                { name = "emoji", insert = true }, -- emoji completion
            },
            completion = {
                keyword_length = 1,
                completeopt = "menu,noselect",
            },
            view = {
                entries = "custom",
            },
            formatting = {
                format = lspkind.cmp_format {
                    mode = "symbol_text",
                    menu = {
                        nvim_lsp = "[LSP]",
                        ultisnips = "[US]",
                        nvim_lua = "[Lua]",
                        path = "[Path]",
                        buffer = "[Buffer]",
                        emoji = "[Emoji]",
                        omni = "[Omni]",
                    },
                },
            },
        }

        -- `:` cmdline setup.
        cmp.setup.cmdline(':', {
            mapping = cmp.mapping.preset.cmdline(),
            sources = cmp.config.sources({
                { name = 'path' }
            }, {
                {
                    name = 'cmdline',
                    option = {
                        ignore_cmds = { 'Man', '!' }
                    }
                }
            })
        })
    end
}}

--  see https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance#how-to-add-visual-studio-code-dark-theme-colors-to-the-menu
-- vim.cmd([[
-- highlight! link CmpItemMenu Comment
-- " gray
-- highlight! CmpItemAbbrDeprecated guibg=NONE gui=strikethrough guifg=#808080
-- " blue
-- highlight! CmpItemAbbrMatch guibg=NONE guifg=#569CD6
-- highlight! CmpItemAbbrMatchFuzzy guibg=NONE guifg=#569CD6
-- " light blue
-- highlight! CmpItemKindVariable guibg=NONE guifg=#9CDCFE
-- highlight! CmpItemKindInterface guibg=NONE guifg=#9CDCFE
-- highlight! CmpItemKindText guibg=NONE guifg=#9CDCFE
-- " pink
-- highlight! CmpItemKindFunction guibg=NONE guifg=#C586C0
-- highlight! CmpItemKindMethod guibg=NONE guifg=#C586C0
-- " front
-- highlight! CmpItemKindKeyword guibg=NONE guifg=#D4D4D4
-- highlight! CmpItemKindProperty guibg=NONE guifg=#D4D4D4
-- highlight! CmpItemKindUnit guibg=NONE guifg=#D4D4D4
-- ]])
